package the.workshop.bu.service.support;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.util.DateUtils;
import the.workshop.bu.entity.Product;
import the.workshop.bu.mapper.ProductMapper;
import the.workshop.bu.mapper.ProductRuleMapper;
import the.workshop.bu.service.dto.ProductMatchDTO;
import the.workshop.bu.service.dto.ProductMatchResultDTO;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ProductMatchSupport {

    @Resource
    private ProductMapper productMapper;

    public List<ProductMatchResultDTO> matchProduct(ProductMatchDTO dto) {
        List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>().orderByDesc(Product::getCreatedTime));
        if (CollectionUtils.isEmpty(productList)) {
            return null;
        }

        List<ProductMatchResultDTO> resultList = new ArrayList<>();
        ProductMatchResultDTO result = null;
        for (Product product : productList) {
            if (dto.getProductType() != null
                    && !dto.getProductType().equals(product.getProductType())) {
                continue;
            }

            if (dto.getCompanyDate() != null) {
                int months = DateUtils.dateCal(dto.getCompanyDate(), new Date(), DateUtils.MONTH_TYPE);
                if (product.getRegisterDay() != null && product.getRegisterDay() >= months) {
                    continue;
                }
            }

            if (dto.getBorrowerIdentity() != null) {
                if (dto.getBorrowerIdentity() == 1) {
                    if (dto.getBorrowerAge() != null ) {
                        if (product.getLegalPersonAgeStart() != null && dto.getBorrowerAge() < product.getLegalPersonAgeStart()) {
                            continue;
                        }

                        if (product.getLegalPersonAgeEnd() != null && dto.getBorrowerAge() > product.getLegalPersonAgeEnd()) {
                            continue;
                        }
                    }
                } else if (dto.getBorrowerIdentity() == 2) {
                    if (product.getIfShareholder() != null && product.getIfShareholder() == 0) {
                        continue;
                    }
                }
            }

            if (dto.getIfLegalPersonChanged() != null) {
                if (dto.getIfLegalPersonChanged() == 1
                        && dto.getLegalPersonChangedMonth() != null
                        && product.getLegalPersonChange() != null
                        && product.getLegalPersonChange() > dto.getLegalPersonChangedMonth()) {
                    continue;
                }
            }

            if (dto.getLastTaxLevel() != null
                    && product.getTaxLevel() != null
                    && !product.getTaxLevel().contains(dto.getLastTaxLevel())) {
                continue;
            }

            if (dto.getIfTaxStatus() != null
                    && product.getIfTaxStatus() != null
                    && product.getIfTaxStatus() == 1
                    && dto.getIfTaxStatus() == 0) {
                continue;
            }

            if (dto.getIfTaxArrearsHistory() != null
                    && product.getIfTaxArrearsHistory() != null
                    && product.getIfTaxArrearsHistory() == 0
                    && dto.getIfTaxArrearsHistory() == 1) {
                continue;
            }

            if (dto.getTaxDuration() != null
                    && product.getTaxDuration() != null
                    && dto.getTaxDuration() < product.getTaxDuration()) {
                continue;
            }

            if (dto.getTaxSalesYear() != null
                    && product.getTaxSalesYear() != null
                    && dto.getTaxSalesYear().compareTo(product.getTaxSalesYear()) < 0) {
                continue;
            }

            if (dto.getInvoiceDuration() != null
                    && product.getInvoiceDuration() != null
                    && dto.getInvoiceDuration() < product.getInvoiceDuration()) {
                continue;
            }

            if (dto.getInvoiceAmountYear() != null
                    && (product.getInvoiceYearStart() != null && dto.getInvoiceAmountYear().compareTo(product.getInvoiceYearStart()) < 0
                        || product.getInvoiceYearEnd() != null && dto.getInvoiceAmountYear().compareTo(product.getInvoiceYearEnd()) >0) ) {
                continue;
            }

            if (dto.getInvoiceCountYear() != null
                    && product.getInvoiceCountYear() != null
                    && dto.getInvoiceCountYear() < product.getInvoiceCountYear()) {
                continue;
            }


            result = new ProductMatchResultDTO();
            result.setProductId(product.getId());
            result.setProductName(product.getName());
            result.setInterestYear(product.getInterestYear());
            result.setQuota(product.getQuota());
            result.setLogo(product.getLogo());
            resultList.add(result);
        }

        return resultList;
    }
}
